package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNoSelectedException;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.SetMealDishMapper;
import com.sky.mapper.SetMealMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import com.sky.service.SetMealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetMealServiceImpl implements SetMealService {
    @Autowired
    SetMealMapper setMealMapper;

    @Autowired
    SetMealDishMapper setMealDishMapper;

    @Autowired
    CategoryMapper categoryMapper;

@Transactional
@CacheEvict(cacheNames = "setmealCahe",key = "#setmealDTO.categoryId")
    @Override
    public void insert(SetmealDTO setmealDTO) {
        Setmeal setmeal = Setmeal.builder().build();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //向套餐表setmeal插入新数据
        setMealMapper.insert(setmeal);

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
       setmealDishes.stream().map((item)->{
           item.setSetmealId(setmeal.getId());
           return item;
       }).collect(Collectors.toList());

        //将菜品和分类的关系数据插入中间表setmeal_dish
        setMealDishMapper.insert(setmealDishes);


    }


    @Override
    public PageResult selectByPage(Integer categoryId,String name,Integer page ,Integer pageSize,Integer status) {
        PageHelper.startPage(page,pageSize);
     List<SetmealVO> setmealVOList=   setMealMapper.selectByPage(name,categoryId,status);

     setmealVOList.stream().map((item)->{
         Long categoryId1 = item.getCategoryId();
         String categoryName = categoryMapper.getNameById(categoryId1);
         item.setCategoryName(categoryName);

         return item;
     }).collect(Collectors.toList());

     Page<SetmealVO> setmealVOPage = (Page<SetmealVO>) setmealVOList;

        PageResult pageResult = new PageResult(setmealVOPage.getTotal(), setmealVOPage.getResult());
        return pageResult;
    }

    @Transactional
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    @Override
    public void update(SetmealDTO setmealDTO) {
        Long setMealId = setmealDTO.getId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.stream().map((item)->{
            item.setSetmealId(setMealId);
            return item;
        }).collect(Collectors.toList());

        Setmeal setmeal = Setmeal.builder().build();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //更新套餐表
        setMealMapper.update(setmeal);
        //更新套餐菜品表
        setMealDishMapper.deleteBySetMealId(setMealId);
        if (setmealDishes!=null&& setmealDishes.size()>0){
            setMealDishMapper.insert(setmealDishes);
        }
    }

    @Override
    public SetmealVO getById(Long id) {
        SetmealVO setmealVO=setMealMapper.selectById(id);
        List<SetmealDish> setmealDishes= setMealDishMapper.selectDishesByDishId(id);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    @Override
    public void changeStatus(Integer status, Long id) {
        Setmeal setmeal = Setmeal.builder()
                .status(status)
                .id(id)
                .build();
        setMealMapper.update(setmeal);
    }

    @Transactional
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    @Override
    public void deleteByIds(List<Long> ids) {
    if (ids==null||ids.size()==0){
        //没有勾选需要批量删除的菜品
        throw new DeletionNoSelectedException(MessageConstant.NO_SELECTED_EXCEPTION);
    }
        for (int i = 0; i < ids.size(); i++) {
            Integer status= setMealMapper.getStatusById(ids.get(i));
            if (status== StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }
        setMealMapper.deleteByIds(ids);
        setMealDishMapper.deleteBySetMealIds(ids);
    }



    /**
     * 条件查询
     * @param setmeal
     * @return
     */

    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setMealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setMealMapper.getDishItemBySetmealId(id);
    }
}
